package req

import (
	"fmt"
	"strconv"
	"strings"
)

type IdsReq struct {
	Ids []string `form:"ids" binding:"required" json:"ids" db:"ids" comment:"主键ID集合"`
}

func (r *IdsReq) ToInt64Slice() ([]int64, error) {
	if len(r.Ids) == 0 {
		return []int64{}, nil
	}

	result := make([]int64, len(r.Ids))
	for i, idStr := range r.Ids {
		// 确保处理前后空格
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			return nil, fmt.Errorf("ID 不能为空 at index %d", i)
		}

		id, err := strconv.ParseInt(idStr, 10, 64)
		if err != nil {
			return nil, fmt.Errorf("无效的 ID '%s' at index %d", idStr, i)
		}
		result[i] = id
	}
	return result, nil
}

// 带错误详细信息的方法
func (r *IdsReq) ToInt64SliceWithDetail() ([]int64, []error) {
	if len(r.Ids) == 0 {
		return []int64{}, nil
	}

	result := make([]int64, len(r.Ids))
	var errors []error

	for i, idStr := range r.Ids {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			errors = append(errors, fmt.Errorf("索引 %d: ID 不能为空", i))
			continue
		}

		id, err := strconv.ParseInt(idStr, 10, 64)
		if err != nil {
			errors = append(errors, fmt.Errorf("索引 %d: 无效的 ID '%s'", i, idStr))
			continue
		}

		result[i] = id
	}

	return result, errors
}
